package com.wjjun.customview.window;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.support.annotation.NonNull;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import com.blankj.utilcode.util.KeyboardUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.wjjun.customview.R;

/**
 * 基础对话框
 * Created by 67535 on 2018/5/2.
 */
public abstract class BaseDialog extends Dialog implements View.OnClickListener {

    private Context context;
    protected View rootView;
    protected TextView titleView;
    protected View contentView;
    protected View cancelView;
    protected View confirmView;

    protected OnDialogListener onDialogListener;

    public BaseDialog(@NonNull Context context) {
        this(context, R.style.customDialog);
    }

    public BaseDialog(@NonNull Context context, int themeResId) {
        super(context, themeResId);
        this.context = context;

        setCanceledOnTouchOutside(false);

        initViews();
        initContent();
        initData();
    }

    protected void initViews() {
        LayoutInflater inflater = LayoutInflater.from(context);
        rootView = inflater.inflate(getLayoutId(), null);
        setContentView(rootView);

        titleView = findViewById(R.id.dialog_title);
        contentView = findViewById(R.id.dialog_content);
        cancelView = findViewById(R.id.dialog_cancel);
        confirmView = findViewById(R.id.dialog_confirm);

        if (cancelView != null) {
            cancelView.setOnClickListener(this);
        }
        if (confirmView != null) {
            confirmView.setOnClickListener(this);
        }
    }

    protected abstract int getLayoutId();

    protected void initContent() {

    }

    protected void initData() {

    }

    protected String getString(int resId) {
        if (context != null) {
            return context.getString(resId);
        }
        return "";
    }

    protected String getString(int resId, Object... arg) {
        if (context != null) {
            return context.getString(resId, arg);
        }
        return "";
    }

    protected void showToast(int resId) {
        ToastUtils.showShort(resId);
    }

    protected void showToast(String msg) {
        ToastUtils.showShort(msg);
    }

    public final <T extends View> T findViewById(int id) {
        if (id == 0) {
            return null;
        }
        return rootView.findViewById(id);
    }

    public BaseDialog setTitleByRes(int resId) {
        if (titleView != null) {
            titleView.setText(resId);
        }
        return this;
    }

    public BaseDialog setTitle(String title) {
        if (titleView != null) {
            titleView.setText(title);
        }
        return this;
    }

    public BaseDialog setCancelText(int resId) {
        if (cancelView != null && cancelView instanceof TextView) {
            ((TextView) cancelView).setText(resId);
        }
        return this;
    }

    public BaseDialog setConfirmText(int resId) {
        if (confirmView != null && confirmView instanceof TextView) {
            ((TextView) confirmView).setText(resId);
        }
        return this;
    }

    @Override
    public void onClick(View v) {
        if (getCurrentFocus() != null) {
            KeyboardUtils.hideSoftInput(getCurrentFocus());
        }
        if (v.getId() == R.id.dialog_cancel) {
            onCancel();
        } else if (v.getId() == R.id.dialog_confirm) {
            onConfirm();
        }
    }

    protected void onCancel() {
        if (onDialogListener != null) {
            onDialogListener.onCancel();
        }else {
            dismiss();
        }
    }

    protected void onConfirm() {
        if (onDialogListener != null) {
            onDialogListener.onConfirm(getConfirmValue());
        }else {
            dismiss();
        }
    }

    protected Object getConfirmValue() {
        return null;
    }

    public OnDialogListener getOnDialogListener() {
        return onDialogListener;
    }

    public BaseDialog setOnDialogListener(OnDialogListener onDialogListener) {
        this.onDialogListener = onDialogListener;
        return this;
    }

    @Override
    public void show() {
        if (context instanceof Activity) {
            KeyboardUtils.hideSoftInput((Activity) context);
        }
        super.show();
    }

    public interface OnDialogListener {
        void onCancel();

        void onConfirm(Object value);
    }
}
